package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.entity.Author;
import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.Login;
import com.example.demo.entity.requestJson.AForgetPwdReq;
import com.example.demo.entity.requestJson.AUpdatePwdReq;
import com.example.demo.entity.requestJson.AuthorInfoReq;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.AuthorMapper;
import com.example.demo.mapper.LoginMapper;
import com.example.demo.service.APersonalSettingsService;
import com.example.demo.tools.Md5utils;
import com.example.demo.tools.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

/**
 * 作者个人设置Service层
 *
 * @author 杨丽辉
 * @since 2021-02-23 17:48:33
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class APersonalSettingsServiceImpl implements APersonalSettingsService {
    @Autowired
    private AuthorMapper authorMapper;
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public BackResponseEntity<Object> updateAuthorInfo (AuthorInfoReq authorInfoReq){
        String authorId = null;
        String authorNickname = null;
        String authorInfo = null;
        String authorImg = null;
        Integer authorSex = null;

        if (!ObjectUtils.isEmpty(authorInfoReq.getAuthorId())) {
            authorId = authorInfoReq.getAuthorId()
                    .trim();
        }
        if (!ObjectUtils.isEmpty(authorInfoReq.getAuthorNickname())) {
            authorNickname = authorInfoReq.getAuthorNickname()
                    .trim();
        }
        if (!ObjectUtils.isEmpty(authorInfoReq.getAuthorImg())) {
            authorImg = authorInfoReq.getAuthorImg()
                    .trim();
        }
        if (!ObjectUtils.isEmpty(authorInfoReq.getAuthorInfo())) {
            authorInfo = authorInfoReq.getAuthorInfo()
                    .trim();
        }
        if (!ObjectUtils.isEmpty(authorInfoReq.getAuthorSex())) {
            authorSex = authorInfoReq.getAuthorSex();
        }

        if(ObjectUtils.isEmpty(authorId) && (ObjectUtils.isEmpty(authorNickname) || ObjectUtils.isEmpty(authorImg) || ObjectUtils.isEmpty(authorInfo) || ObjectUtils.isEmpty(authorSex))){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006,"参数缺失");
        }

        LocalDateTime time = LocalDateTime.now();
        LambdaUpdateWrapper<AuthorInfoReq> wrappers = new UpdateWrapper<AuthorInfoReq>().lambda();

        if(authorMapper.countNickname(authorNickname) != 0){
            return BackResponseEntity.success(ErrCodeEnum.unknown_1040, "昵称已存在");
        }

        Author author = new Author();
        author.setAuthorId(authorId);
        author.setAuthorImg(authorImg);
        author.setAuthorNickname(authorNickname);
        author.setAuthorSex(authorSex);
        author.setAuthorInfo(authorInfo);
        author.setUpdateTime(time);

        Login login = new Login();
        login.setLoginId(authorId);
        login.setLoginPic(authorImg);
        login.setLoginNickname(authorNickname);
        login.setUpdateTime(time);

        int i = authorMapper.updateAuthor(author);
        int m = loginMapper.updateLogin(login);
        if( i > 0 && m > 0){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0,"修改个人信息成功！");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_4005,"修改个人信息失败");
        }
    }

    @Override
    public BackResponseEntity<Object> updatePwd(AUpdatePwdReq updatePwdReq){
        String id = null;
        String pwd = null;
        String newPwd = null;
        String confirmPwd = null;

        if(!ObjectUtils.isEmpty(updatePwdReq.getAuthorId())){
            id = updatePwdReq.getAuthorId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(updatePwdReq.getPwd())){
            pwd = updatePwdReq.getPwd()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(updatePwdReq.getNewPwd())){
            newPwd = updatePwdReq.getNewPwd()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(updatePwdReq.getConfirmPwd())){
            confirmPwd = updatePwdReq.getConfirmPwd()
                    .trim();
        }
        if(ObjectUtils.isEmpty(id) || ObjectUtils.isEmpty(pwd) || ObjectUtils.isEmpty(newPwd) || ObjectUtils.isEmpty(confirmPwd)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006,"参数缺失");
        }

        Login loginRes = loginMapper.selectById(id);

        String md5Pwd = Md5utils.getmd5Lowercase("qingyu" + pwd);

        if(!md5Pwd.equals(loginRes.getLoginPwd())){
            return BackResponseEntity.success(ErrCodeEnum.unknown_1007,"原密码输入错误");
        }
        if(!newPwd.equals(confirmPwd)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_1002,"两次输入密码不一致");
        }
        if(Md5utils.getmd5Lowercase("qingyu" + newPwd).equals(loginRes.getLoginPwd())){
            return BackResponseEntity.success(ErrCodeEnum.unknown_1008, "新密码不能与旧密码一致");
        }

        LocalDateTime time = LocalDateTime.now();
        Login login = new Login();
        login.setLoginPwd(Md5utils.getmd5Lowercase("qingyu" + newPwd));
        login.setLoginId(id);
        login.setUpdateTime(time);

        int i = loginMapper.updateLogin(login);

        if(i > 0){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0,"修改密码成功,请重新登录");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_4005,"修改密码失败");
        }
    }

    @Override
    public BackResponseEntity<Object> forgetPwd(AForgetPwdReq aForgetPwdReq){
        String id = null;
        String code = null;
        String newPwd = null;
        String cofirmPwd = null;
        String phone = null;

        if(!ObjectUtils.isEmpty(aForgetPwdReq.getAuthorId())){
            id = aForgetPwdReq.getAuthorId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aForgetPwdReq.getCode())){
            code = aForgetPwdReq.getCode()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aForgetPwdReq.getNewPwd())){
            newPwd = aForgetPwdReq.getNewPwd()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aForgetPwdReq.getConfirmPwd())){
            cofirmPwd = aForgetPwdReq.getConfirmPwd()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aForgetPwdReq.getPhone())){
            phone = aForgetPwdReq.getPhone()
                    .trim();
        }
        if(ObjectUtils.isEmpty(id) || ObjectUtils.isEmpty(code) || ObjectUtils.isEmpty(newPwd) || ObjectUtils.isEmpty(cofirmPwd) || ObjectUtils.isEmpty(phone)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        Object verifyDataObject = redisUtil.get(phone + "SMS:authors");
        Login loginRes = loginMapper.selectById(id);

        if (!ObjectUtils.isEmpty(verifyDataObject)) {
            String verifyData = (String) verifyDataObject;
            if (!code.equals(verifyData)) {
                return BackResponseEntity.success(ErrCodeEnum.unknown_1001,"验证码错误");
            }
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1000,"验证码过期");
        }

        if(!newPwd.equals(cofirmPwd)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_1002,"两次输入密码不一致");
        }

        if(Md5utils.getmd5Lowercase("qingyu" + newPwd).equals(loginRes.getLoginPwd())){
            return BackResponseEntity.success(ErrCodeEnum.unknown_1008, "新密码不能与旧密码一致");
        }
        LocalDateTime time = LocalDateTime.now();
        Login login = new Login();
        login.setLoginPwd(Md5utils.getmd5Lowercase("qingyu" + newPwd));
        login.setLoginId(id);
        login.setUpdateTime(time);

        int i = loginMapper.updateLogin(login);

        if(i > 0){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0,"重置密码成功,请重新登录");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_4005,"重置密码失败");
        }

    }
}
